<?php

namespace app\index\controller;

use think\exception\ValidateException;
use app\model\ConsignGoods;
use app\model\Goods;
use app\model\GoodsImage;
use app\model\GoodsDescription;

class ConsigngoodsController extends Base
{
    public function index()
    {
        $weid = weid();
        $page = input('post.page', 1, 'intval');
        $keyword = input('post.keyword', '', 'serach_in');
        $ocid = input('post.ocid', '', 'serach_in');

        if (empty($ocid)) {
            $ocid = $this->ocid;
        }

        $query = ConsignGoods::where(['weid' => $weid, 'status' => 1]);

        $priceOrder = input('post.priceOrder');
        $salesOrder = input('post.salesOrder');

        if (!empty($priceOrder)) {
            $Sort = 'price ' . $priceOrder;
        } elseif (!empty($salesOrder)) {
            $Sort = 'sale_count ' . $salesOrder;
        } else {
            $Sort = 'sort asc,id desc';
        }

        if (!empty($ocid)) {
            $query->where('ocid', $ocid);
        }

        if (!empty($keyword)) {
            $query->where('title', 'like', '%' . $keyword . '%');
        }

        $res = $query->order($Sort)
            ->paginate(getpage())
            ->toArray();

        foreach ($res['data'] as &$vo) {
            $vo['create_time'] = time_format($vo['create_time']);
            $vo['update_time'] = time_format($vo['update_time']);
        }
        $res['data'] = ConsignGoods::setGoodslist($res['data']);

        $data['data'] = $res;

        return $this->json($data);
    }
    public function detail($id)
    {
        $cid = input('get.cid');

        if (!empty($cid)) {
            $consign = ConsignGoods::find($cid);
            if (!empty($consign)) {
                $consign = $consign->toArray();
            }
        }
        //增加点击
        Goods::where('id', $id)->inc('viewed')->update();

        $goodsdata = Goods::goodsInfo($id);
        if (!empty($tuan['price'])) {
            $goodsdata['original_price'] = $goodsdata['price'];
            $goodsdata['price'] = $tuan['price'];
        }
        if (!empty($consign['price'])) {
            $goodsdata['original_price'] = $goodsdata['price'];
            $goodsdata['price'] = $consign['price'];
        }

        $data = $goodsdata;
        $picdataarray = GoodsImage::where(['goods_id' => $id])
            ->order('id asc')
            ->select()
            ->toArray();

        foreach ($picdataarray as &$vo) {
            $vo['pic'] = toimg($vo['image']);
        }

        $description = GoodsDescription::where(['goods_id' => $id])->find();

        if ($data['quantity'] < 0) {
            $data['quantity'] = 0;
        }

        $data['minPrice'] = $goodsdata['price'];
        $data['pic'] = toimg($goodsdata['image']);
        $data['minPoints'] = $goodsdata['pay_points'];
        $data['stores'] = $goodsdata['quantity'];

        $data['sale_count'] = $goodsdata['sale_count'] + $goodsdata['sale_count_base'];
        $data['viewed'] = $goodsdata['viewed'] + $goodsdata['viewed_base'];

        $data['consign'] = $consign;
        $data['price'] = $goodsdata['price'];
        $data['points'] = $goodsdata['pay_points'];
        $data['content'] = sethtmlimg($description->description);
        $data['pics'] = $picdataarray;
        return $this->json(['data' => $data]);
    }
}
